package com.rxmvp.rxmvpdemo.contract.main;

import android.util.Log;

import com.rxmvp.rxmvpdemo.RxBus.Events;
import com.rxmvp.rxmvpdemo.RxBus.RxBus;
import com.rxmvp.rxmvpdemo.RxRetrofit.NetApi;
import com.rxmvp.rxmvpdemo.RxRetrofit.Response.BaseResponse;
import com.rxmvp.rxmvpdemo.RxRetrofit.RxRetrofitUtils;
import com.rxmvp.rxmvpdemo.RxRetrofit.subscriber.ResponseSubscriber;
import com.rxmvp.rxmvpdemo.base.RxBaseActivityPresenter;
import com.rxmvp.rxmvpdemo.bean.NewsBean;
import com.trello.rxlifecycle.ActivityEvent;
import com.trello.rxlifecycle.components.ActivityLifecycleProvider;

import javax.inject.Inject;

import rx.Subscription;
import rx.functions.Action1;

/**
 * MainActivity的presenter，负责处理MainActivity的业务逻辑，减轻MainActivity的代码量，也起到与界面交互解耦的作用，方便单元测试
 *
 * 作者： huangRZ on 2017/1/21. 15:04
 * 邮箱：917647409@qq.com
 */
public class MainPresenter extends RxBaseActivityPresenter<MainContract.IView> implements MainContract.IPresenter{

    private NetApi api;
    private RxRetrofitUtils mRxRetrofit;
    private Subscription mNewSubscription;

    //提供MainPresenter的实例，其参数的实例由注入到MainActivity的component管理的module提供
    @Inject
    public MainPresenter(MainContract.IView view, ActivityLifecycleProvider activityLifecycleProvider,
                         NetApi api, RxRetrofitUtils rxRetrofit) {
        super(view, activityLifecycleProvider);
        this.api = api;
        this.mRxRetrofit = rxRetrofit;
    }

    @Override
    public void onStart() {
        RxBus.with(mActivityProvider)
                .setLifecycleEvent(ActivityEvent.DESTROY)
                .setEvent(Events.TAP)
                .onError(new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showShortToast(throwable.getMessage());
                    }
                })
                .onNext(new Action1<Events<?>>() {
                    @Override
                    public void call(Events<?> events) {
//                        NewsBean newsBean = (NewsBean) events.getContent();
//                        tv.setText(newsBean.toString());
                        Log.e("tag", "response : " + events.getContent());
                        mView.setText((String) events.getContent());
                    }
                })
                .create();
    }

    @Override
    public void onStop() {

    }

    public void loadData() {
        mNewSubscription = mRxRetrofit
                .setLifecycleEvent(ActivityEvent.DESTROY) //设置subscriber在Activity销毁时自动解绑
                .getObservable(api.getJson("top", "004a2b29ab3245fdfb95ed25e590cc2b"), mActivityProvider)
                .subscribe(new ResponseSubscriber<NewsBean>() {
                    @Override
                    public void onSuccess(BaseResponse<NewsBean> response) {
                        mView.setText(response.getResult().getData().get(0).toString());
                        Events<String> stringEvents = new Events<>();
                        stringEvents.setCode(Events.TAP);
                        stringEvents.setContent(response.getResult().getData().get(0).toString());
                        RxBus.getInstance().send(stringEvents);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        mView.showPB();
                    }

                    @Override
                    public void onFailure() {
                        super.onFailure();
                        mView.hidePB();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mView.hidePB();
                    }
                });
    }

    //取消所有订阅了的subscriber，避免内存泄漏
    @Override
    public void cancelSubscription() {
        if (!mNewSubscription.isUnsubscribed()) {
            mNewSubscription.unsubscribe();
            mView.hidePB();
        }
    }
}
